import Cookie from 'js-cookie';
import md5 from 'js-md5';
// token认证 
interface HWkey {
  HWAppKey: string;
  HWAppSecret: string; 
  HWAppId: number;  
  HWRequestTimeStamp: number;
} 
const xsrfHeaderName1: string = 'access-token'; 
const xsrfHeaderName2: string = 'x-access-token';
const HwkeyInfo: HWkey = {
  HWAppKey: 'bf5587238e13a51c45f66caf4f6d874c',
  HWAppSecret:import.meta.env.MODE === 'development'? 'b2a15d1852e08daba422780088472fb5':'040c4ad1d967189fa13957dfdf5eada8',
  HWAppId: 369462942949445, 
  HWRequestTimeStamp: new Date().getTime(),  
};   
setInterval(() => {   
  // console.log('触发')  
  HwkeyInfo.HWRequestTimeStamp = new Date().getTime();
}, 2000);
/**
 * 设置认证信息
 * @param auth {Object}
 * @param authType {AUTH_TYPE} 认证类型，默认：{AUTH_TYPE.BEARER}
 */
function setAuthorization(auth: any): void { 
  for (const key in auth) {
    // key:string 不能分配给 { name:string; age:number }类型 

    if (key == 'access-token') {    
      if (auth[key]) { 
        Cookie.set(xsrfHeaderName1, 'Bearer ' + auth[key], { path: '/', domain: '.jiyi.show' });
        // Cookie.set(xsrfHeaderName1, 'Bearer ' + auth[key]); 
        //Cookie.set(xsrfHeaderName1, 'Bearer ' + auth[key]);  
      } else {  
        Cookie.set(xsrfHeaderName1, '', { path: '/', domain: '.jiyi.show' });
      } 
    }  
    if (key == 'x-access-token') {   
      if (auth[key]) { 
        Cookie.set(xsrfHeaderName2, 'Bearer ' + auth[key], { path: '/', domain: '.jiyi.show' });
       // Cookie.set(xsrfHeaderName2, 'Bearer ' + auth[key]);
        // Cookie.set(xsrfHeaderName2, 'Bearer ' + auth[key]); 
      } else { 
        Cookie.set(xsrfHeaderName2, '', { path: '/', domain: '.jiyi.show' });
      }
    }  
  }   
}   
      
//删除cokie  t-www.jiyi.show 
function removeAuthorization(): void {      
  Cookie.remove(xsrfHeaderName1);       
  Cookie.remove(xsrfHeaderName2);     
  Cookie.remove(xsrfHeaderName1, { path: '/', domain: '.jiyi.show' });
  Cookie.remove(xsrfHeaderName2, { path: '/', domain: '.jiyi.show' });  
      
} 
//获取cokie
function getHeardToken(): String {
  let token: string | null = Cookie.get(xsrfHeaderName1);
  return token;
} 
function getHeardXtoken(): String {
  let token: string | null = Cookie.get(xsrfHeaderName2);
  return token;
}
function getTokenStatus(): boolean {
  let token: string | null = Cookie.get(xsrfHeaderName1);
  let Xtoken: string | null = Cookie.get(xsrfHeaderName2);
  if (token && Xtoken) {
    return true;
  }
  return false;
}

function setMd5Info(): HWkey {
  let str: string =
    HwkeyInfo.HWAppSecret +
    '&' +
    HwkeyInfo.HWAppId +
    '&' +
    HwkeyInfo.HWRequestTimeStamp +
    '&' +
    HwkeyInfo.HWAppKey +
    '&' +
    HwkeyInfo.HWAppSecret;

  str = md5(str);
  let obj = {
    HWAppKey: HwkeyInfo.HWAppKey,
    HWAppSecret: str,
    HWAppId: HwkeyInfo.HWAppId,
    HWRequestTimeStamp: HwkeyInfo.HWRequestTimeStamp,
  };
  return obj;
}
export { removeAuthorization, setMd5Info, setAuthorization, getHeardToken, getHeardXtoken, getTokenStatus };
